package cn.baiyang.rocketmq.example;

import javax.annotation.Resource;

import org.apache.rocketmq.client.apis.ClientException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.baiyang.rocketmq.example.producer.TemplateExample;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author hongzhu
 * @version V1.0
 * @since 2023-03-13 10:00
 */
@Slf4j
@RestController
@RequestMapping(path = "/sending", consumes = "application/json", produces = "application/json")
@ConditionalOnProperty(prefix = "rocketmq-java", value = {"producer.topics"})
public class TestSendingController {

    @Resource
    private TemplateExample templateExample;

    @GetMapping(path = "/test")
    public String test() {
        for (int i = 1; i <= 10; i++) {
            String body = "{\"name\": \"zhangsan\", \"phone\": \"13305712345\"}";

            try {
                String messageId = templateExample.testSending(body);
                log.info("{}. Message is Sent, Id={}", i, messageId);
            } catch (ClientException e) {
                log.error("{}. Failed to sending", i, e);
                return "{\"code\": -1, \"message\": \"failed\"}";
            }

        }
        return "{\"code\": 0, \"message\": \"success\"}";
    }

}
